﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//技能操作面板
public class SkillAtkDlg
{
    GameObject _root;
    List<SkillPanel> _allSkillPanel = new List<SkillPanel>();
    public Action<int> OnSkillBtnClick;
    //技能索引，CD百分比
    public Func<int, float> OnGetSkillCD;

    public SkillAtkDlg()
    {
        _root = UIMgr.GetInstance().Add("UI/FightUI/SkillAtkDlg", UILayer.FightUI);
        for(int i = 0; i < GameSetting.MaxSkillNum; ++i)
        {
            var skillPanel = new SkillPanel();
            var index = (i + 1);
            string panelName = "SkillPanel" + (i + 1); 

            skillPanel.Btn = _root.Find<Button>(panelName);
            skillPanel.CountDown = _root.Find<Image>(panelName+"/CountDownImage");
            skillPanel.CDText = _root.Find<Text>(panelName+"/CountDownLabel");
            skillPanel.Btn.onClick.AddListener(()=> onSkillClick(index));
            _allSkillPanel.Add(skillPanel);
        }
        TimerMgr.GetInstance().CreateTimerAndStart(0.08f, -1, UpdateCD);
    }

    //计时器更新CD
    private void UpdateCD()
    {
        if (OnGetSkillCD == null) { return; }
       
        for(int i = 0; i < _allSkillPanel.Count; ++i)
        {
            setSkillCD(_allSkillPanel[i], OnGetSkillCD(i+1));
        }
    }

    private void onSkillClick(int index)
    {
        if (OnSkillBtnClick != null)
        {
            OnSkillBtnClick(index);
        }
    }
    private void setSkillCD(SkillPanel skillPanel,float percent)
    {
        skillPanel.CountDown.fillAmount = percent;
    }
}
//技能面板中，单个技能相关组件打包
public class SkillPanel
{
    public Button Btn;
    public Image CountDown;
    public Text CDText;
}